import sys
import time
import re
import pyperclip

import settings

interval = settings.clipboard_refresh_interval

str_type = type('str')


def __same_clip_content(prev, curr):
    count = 1

    if len(prev) == len(curr):
        count = len(prev) - 1
        while count > 0 and prev[count] == curr[count]:
            count -= 1
        
    return count == 0 and prev[1] == curr[1]


def __http_format_validation(text):
    return not re.search(settings.http_format_pattern, text) is None


def get_clip_link():

    content_prev = "http://"
    content_curr = "http://"
    validation = False

    while not validation:
        content_prev = pyperclip.getcb()
        time.sleep(interval)
        content_curr = pyperclip.getcb()

        if type(content_prev) is str_type and type(content_curr) is str_type:
            validation = __http_format_validation(content_curr) and not __same_clip_content(content_prev, content_curr)

    return content_curr


if __name__ == '__main__':
    while True:
        link = get_clip_link()
        print link
